﻿using CommonModel.Field;
using CommonModel.Model;
using CommonModel.Util;
using GXRTBTC.PublicClassLibrary.Field;
using GXRTBTC.PublicClassLibrary.Interface;
using GXRTBTC.PublicClassLibrary.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GXRTBTC.PublicClassLibrary.ParseImpl
{
    public class ConvertParse_SNMP_Impl : IConvertParse
    {
        public string mVersion { get; } = "ConvertParse_SNMP_Impl";
        public Dictionary<string, SchemaDocument> schemaDict { get; set; }
        /// <summary>
        /// 未知，无法解析的
        /// </summary>
        public Dictionary<string, int> unknownSchemaName { get; set; } = new Dictionary<string, int>();

        public ConvertParse_SNMP_Impl()
        { 
        
        }

        /// <summary>
        /// 获取OID匹配
        /// </summary>
        /// <returns></returns>
        private Dictionary<string, string> GetOids(SchemaDocument doc)
        {
            Dictionary<string, string> oidsMahch = new Dictionary<string, string>();
            Dictionary<int, SchemaDictionary> fieldDictionary = doc.fieldDictionary;
            //根据配置文件循环
            for (int i = 0; i < fieldDictionary.Count; i++)
            {
                //object schObj = fieldDictionary[i];

                //如果是普通字段，且字节位标识bit字段
                if (!fieldDictionary[i].isNode)
                {
                    oidsMahch.TryAdd(fieldDictionary[i].code, fieldDictionary[i].name);
                }
            }
            return oidsMahch;
        }

        /// <summary>
        /// 转换
        /// </summary>
        /// <param name="packetInfo"></param>
        /// <returns></returns>
        public Dictionary<string, object> DataConvert(PacketInfo packetInfo)
        {
            //头信息
            HeaderData header = packetInfo.header;

            string schemaName = header.device + "_" + header.vendor + "_" + header.msgType;
            SchemaDocument doc;
            Dictionary<string, object> parsedDataDictionary = new Dictionary<string, object>();
            if (schemaDict.ContainsKey(schemaName))
            {
                if (unknownSchemaName.ContainsKey(schemaName))
                {
                    unknownSchemaName.Remove(schemaName);
                }

                doc = schemaDict[schemaName];// 获取对应的xml解析文件
                //关联配置，精简schemaDict的空间占用
                if (doc.rname != null && !"".Equals(doc.rname))
                {
                    if (schemaDict.ContainsKey(doc.rname))
                    {
                        doc = schemaDict[doc.rname];// 获取对应的xml解析文件
                    }
                    else
                    {
                        if (!unknownSchemaName.ContainsKey(doc.rname))
                        {
                            unknownSchemaName.TryAdd(doc.rname, 0);
                        }
                        packetInfo.tipMsg = "解析协议配置【" + doc.rname + "】未找到，请检查file_param_main表";
                        return null;
                    }
                }

                Dictionary<string, string> oidsMatch = GetOids(doc);
                Dictionary<string, string> vals = SNMPUtil.AnalySnmp(packetInfo.reviceDatas);
                foreach (string oid in vals.Keys)
                {
                    if (oidsMatch.ContainsKey(oid))
                    {
                        parsedDataDictionary.TryAdd(oidsMatch[oid], vals[oid]);
                    }
                    else
                    {
                        parsedDataDictionary.TryAdd(oid, vals[oid]);
                    }
                }
            }
            else
            {
                if (!unknownSchemaName.ContainsKey(schemaName))
                {
                    unknownSchemaName.TryAdd(schemaName, 0);
                }
                packetInfo.tipMsg = "解析协议配置【" + schemaName + "】未找到，请检查file_param_main表";
                return null;
            }

            //拼接发送的json数据
            Dictionary<string, object> resultDict = new Dictionary<string, object>();//发送下去的总json数据

            Dictionary<string, string> ptlDict = new Dictionary<string, string>();
            ptlDict.Add("ptlver", mVersion);
            ptlDict.Add("ts", Convert.ToInt64(TransForm.GetTimeStamp(packetInfo.receivingTime)).ToString());//时间戳
            ptlDict.Add("ptlnode", "");//协议转换节点

            //相关配置信息
            Dictionary<string, string> addDict = new Dictionary<string, string>();
            addDict.Add("ip", packetInfo.ip);//ip地址
            addDict.Add("port", packetInfo.port.ToString());//端口
            addDict.Add("sid", header.stationcode);//台站id
            addDict.Add("paramId", doc.id);
            ptlDict.Add("file_name", doc.file_name);//配置文件名称
            ptlDict.Add("schemaName", schemaName);//对应的协议文档名字

            resultDict.Add("ptlinfo", ptlDict);
            resultDict.Add("addr", addDict);
            resultDict.Add("datas", parsedDataDictionary);

            return resultDict;
        }

        /// <summary>
        /// 头部识别
        /// </summary>
        /// <param name="packetInfo"></param>
        /// <returns></returns>
        public HeaderData GetHeaderData(PacketInfo packetInfo)
        {
            //byte[] data = TransForm.HexTobyte(packetInfo.reviceDatas);
            HeaderData header = new HeaderData();

            //源数据包起始标识
            header.flag = "30";
            //暂时留空
            header.stationcode = "";
            //软件版本
            header.software = "00"; //默认snmpv1吧
            //硬件版本
            header.software = "";

            //设备类型码 目前是发射机的 暂时写死
            //header.Device = "0x0306";
            //经商量，使用1+1切换器的设备类型码
            header.device = "0x0708";

            //厂家代码 这里默认用协议类型snmp
            header.vendor = "snmp";

            //消息类型 固化为GetBasicMsg
            header.msgType = "GetBasicMsg";

            header.devaddr = packetInfo.port;
            header.classVer = this.mVersion;
            return header;
        }

        public void Dispose()
        {

        }
    }
}
